#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: app_main.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

The startup application for the gui.

"""

#-----------------------------------------------------------------------
# System imports
from threading import Thread

# Application imports
import Pyro.core
from Pyro.EventService.Clients import Subscriber

#======================================================================
# The event collector for the Web GUI
q = {}

def init():
    # When we are properly done
    #Pyro.config.PYRO_HOST = config.pyro_local_host()
    
    # Start the event listener thread for GUI hints
    EventListener().start()

#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
class UiHints(Subscriber):
    
    #---------------------------------------------------------------------------
    def __init__(self):
        Subscriber.__init__(self)
        self.setThreading(0)

    #---------------------------------------------------------------------------
    def event(self, event):
        # Wait for messages
        # Call the appropriate method on the GUI       
        #print 'Received UI Hint', event.msg
        func, args = event.msg
        if func == 'terminate':
          self.abort()
        elif func == 'freq_change':
            q['freq'] = args
        elif func == 'init_freq':
            q['freq_init'] = args
        elif func == 'mode_hints':
            q['mode'] = args
        elif func == 'filter_hints':
            q['filter'] = args
        elif func == 'agc_hints':
            q['agc'] = args
        

#------------------------------------------------------------------------------- 
class EventListener(Thread):
    
    #---------------------------------------------------------------------------
    def __init__(self):
        Thread.__init__(self)
    
    #---------------------------------------------------------------------------    
    def run(self):
        
        Pyro.core.initServer()
        listener = UiHints()
        listener.subscribeMatch("ui_hints")
        listener.listen()
        
 
#-------------------------------------------------------------------------------       
def get_next():
    # Get these in priority order
    if len(q) > 0:
        if q.has_key('freq_init'):
            r = ('freq_init', q['freq_init'])
            q.__delitem__('freq_init')
        elif q.has_key('freq'):
            r = ('freq', q['freq'])
            q.__delitem__('freq')
        elif q.has_key('mode'):
            r = ('mode', q['mode'])
            q.__delitem__('mode')
        elif q.has_key('filter'):
            r = ('filter', q['filter'])
            q.__delitem__('filter')
        elif q.has_key('agc'):
            r = ('agc', q['agc'])
            q.__delitem__('agc')
        return r
    else:
        return None
    

